# Copyright 2023 Google LLC
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

load(
    "//:build_defs.bzl",
    "xnnpack_benchmark",
    "xnnpack_cc_library",
    "xnnpack_unit_test",
)

ACCURACY_EVAL_DEPS = [
    "//:aligned_allocator",
    "//:bench_microkernels",
    "//:common",
    "//:isa_checks",
    "//:math",
    "//:math_stubs",
    "//bench:bench_utils",
    "@FP16",
    "@pthreadpool",
]

#################### Accuracy evaluation for math functions ####################

xnnpack_benchmark(
    name = "f16_exp_ulp_eval",
    srcs = [
        "f16-exp-ulp.cc",
    ],
    copts = select({
        "//:cpuinfo_enabled": ["-DXNN_ENABLE_CPUINFO=1"],
        "//conditions:default": ["-DXNN_ENABLE_CPUINFO=0"],
    }),
    deps = ACCURACY_EVAL_DEPS + select({
        "//:cpuinfo_enabled": ["@cpuinfo"],
        "//conditions:default": [],
    }),
)

xnnpack_benchmark(
    name = "f16_expminus_ulp_eval",
    srcs = [
        "f16-expminus-ulp.cc",
    ],
    copts = select({
        "//:cpuinfo_enabled": ["-DXNN_ENABLE_CPUINFO=1"],
        "//conditions:default": ["-DXNN_ENABLE_CPUINFO=0"],
    }),
    deps = ACCURACY_EVAL_DEPS + select({
        "//:cpuinfo_enabled": ["@cpuinfo"],
        "//conditions:default": [],
    }),
)

xnnpack_benchmark(
    name = "f16_expm1minus_ulp_eval",
    srcs = [
        "f16-expm1minus-ulp.cc",
    ],
    copts = select({
        "//:cpuinfo_enabled": ["-DXNN_ENABLE_CPUINFO=1"],
        "//conditions:default": ["-DXNN_ENABLE_CPUINFO=0"],
    }),
    deps = ACCURACY_EVAL_DEPS + select({
        "//:cpuinfo_enabled": ["@cpuinfo"],
        "//conditions:default": [],
    }),
)

xnnpack_benchmark(
    name = "f16_sigmoid_ulp_eval",
    srcs = [
        "f16-sigmoid-ulp.cc",
    ],
    copts = select({
        "//:cpuinfo_enabled": ["-DXNN_ENABLE_CPUINFO=1"],
        "//conditions:default": ["-DXNN_ENABLE_CPUINFO=0"],
    }),
    deps = ACCURACY_EVAL_DEPS + select({
        "//:cpuinfo_enabled": ["@cpuinfo"],
        "//conditions:default": [],
    }),
)

xnnpack_benchmark(
    name = "f16_sqrt_ulp_eval",
    srcs = [
        "f16-sqrt-ulp.cc",
    ],
    copts = select({
        "//:cpuinfo_enabled": ["-DXNN_ENABLE_CPUINFO=1"],
        "//conditions:default": ["-DXNN_ENABLE_CPUINFO=0"],
    }),
    deps = ACCURACY_EVAL_DEPS + select({
        "//:cpuinfo_enabled": ["@cpuinfo"],
        "//conditions:default": [],
    }),
)

xnnpack_benchmark(
    name = "f16_tanh_ulp_eval",
    srcs = [
        "f16-tanh-ulp.cc",
    ],
    copts = select({
        "//:cpuinfo_enabled": ["-DXNN_ENABLE_CPUINFO=1"],
        "//conditions:default": ["-DXNN_ENABLE_CPUINFO=0"],
    }),
    deps = ACCURACY_EVAL_DEPS + select({
        "//:cpuinfo_enabled": ["@cpuinfo"],
        "//conditions:default": [],
    }),
)

xnnpack_benchmark(
    name = "f32_exp_ulp_eval",
    srcs = [
        "f32-exp-ulp.cc",
    ],
    copts = select({
        "//:cpuinfo_enabled": ["-DXNN_ENABLE_CPUINFO=1"],
        "//conditions:default": ["-DXNN_ENABLE_CPUINFO=0"],
    }),
    deps = ACCURACY_EVAL_DEPS + select({
        "//:cpuinfo_enabled": ["@cpuinfo"],
        "//conditions:default": [],
    }),
)

xnnpack_benchmark(
    name = "f32_expminus_ulp_eval",
    srcs = [
        "f32-expminus-ulp.cc",
    ],
    copts = select({
        "//:cpuinfo_enabled": ["-DXNN_ENABLE_CPUINFO=1"],
        "//conditions:default": ["-DXNN_ENABLE_CPUINFO=0"],
    }),
    deps = ACCURACY_EVAL_DEPS + select({
        "//:cpuinfo_enabled": ["@cpuinfo"],
        "//conditions:default": [],
    }),
)

xnnpack_benchmark(
    name = "f32_expm1minus_ulp_eval",
    srcs = [
        "f32-expm1minus-ulp.cc",
    ],
    copts = select({
        "//:cpuinfo_enabled": ["-DXNN_ENABLE_CPUINFO=1"],
        "//conditions:default": ["-DXNN_ENABLE_CPUINFO=0"],
    }),
    deps = ACCURACY_EVAL_DEPS + select({
        "//:cpuinfo_enabled": ["@cpuinfo"],
        "//conditions:default": [],
    }),
)

xnnpack_benchmark(
    name = "f32_extexp_ulp_eval",
    srcs = [
        "f32-extexp-ulp.cc",
    ],
    copts = select({
        "//:cpuinfo_enabled": ["-DXNN_ENABLE_CPUINFO=1"],
        "//conditions:default": ["-DXNN_ENABLE_CPUINFO=0"],
    }),
    deps = ACCURACY_EVAL_DEPS + select({
        "//:cpuinfo_enabled": ["@cpuinfo"],
        "//conditions:default": [],
    }),
)

xnnpack_benchmark(
    name = "f32_sigmoid_ulp_eval",
    srcs = [
        "f32-sigmoid-ulp.cc",
    ],
    copts = select({
        "//:cpuinfo_enabled": ["-DXNN_ENABLE_CPUINFO=1"],
        "//conditions:default": ["-DXNN_ENABLE_CPUINFO=0"],
    }),
    deps = ACCURACY_EVAL_DEPS + select({
        "//:cpuinfo_enabled": ["@cpuinfo"],
        "//conditions:default": [],
    }),
)

xnnpack_benchmark(
    name = "f32_sqrt_ulp_eval",
    srcs = [
        "f32-sqrt-ulp.cc",
    ],
    copts = select({
        "//:cpuinfo_enabled": ["-DXNN_ENABLE_CPUINFO=1"],
        "//conditions:default": ["-DXNN_ENABLE_CPUINFO=0"],
    }),
    deps = ACCURACY_EVAL_DEPS + select({
        "//:cpuinfo_enabled": ["@cpuinfo"],
        "//conditions:default": [],
    }),
)

xnnpack_benchmark(
    name = "f32_tanh_ulp_eval",
    srcs = [
        "f32-tanh-ulp.cc",
    ],
    copts = select({
        "//:cpuinfo_enabled": ["-DXNN_ENABLE_CPUINFO=1"],
        "//conditions:default": ["-DXNN_ENABLE_CPUINFO=0"],
    }),
    deps = ACCURACY_EVAL_DEPS + select({
        "//:cpuinfo_enabled": ["@cpuinfo"],
        "//conditions:default": [],
    }),
)

################### Accuracy verification for math functions ##################

xnnpack_cc_library(
    name = "math_evaluation_tester",
    testonly = True,
    srcs = ["math-evaluation-tester.cc"],
    hdrs = ["math-evaluation-tester.h"],
    deps = [
        "@FP16",
        "//:aligned_allocator",
        "//:common",
        "//:math",
        "//:math_stubs",
        "@pthreadpool",
        "@com_google_googletest//:gtest_main",
    ],
)

xnnpack_unit_test(
    name = "f16_sqrt_eval",
    srcs = [
        "f16-sqrt.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS,
)

xnnpack_unit_test(
    name = "f16_tanh_eval",
    srcs = [
        "f16-tanh.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS + [
        ":math_evaluation_tester",
    ],
)

xnnpack_unit_test(
    name = "f16_f32_cvt_eval",
    srcs = [
        "f16-f32-cvt.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS,
)

xnnpack_unit_test(
    name = "f32_f16_cvt_eval",
    srcs = [
        "f32-f16-cvt.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS,
)

xnnpack_unit_test(
    name = "f32_qs8_cvt_eval",
    srcs = [
        "f32-qs8-cvt.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS,
)

xnnpack_unit_test(
    name = "f32_qu8_cvt_eval",
    srcs = [
        "f32-qu8-cvt.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS,
)

xnnpack_unit_test(
    name = "f32_exp_eval",
    srcs = [
        "f32-exp.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS + [
        ":math_evaluation_tester",
    ],
)

xnnpack_unit_test(
    name = "f32_expm1minus_eval",
    srcs = [
        "f32-expm1minus.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS + [
        ":math_evaluation_tester",
    ],
)

xnnpack_unit_test(
    name = "f32_expminus_eval",
    srcs = [
        "f32-expminus.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS + [
        ":math_evaluation_tester",
    ],
)

xnnpack_unit_test(
    name = "f32_roundne_eval",
    srcs = [
        "f32-roundne.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS,
)

xnnpack_unit_test(
    name = "f32_roundd_eval",
    srcs = [
        "f32-roundd.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS,
)

xnnpack_unit_test(
    name = "f32_roundu_eval",
    srcs = [
        "f32-roundu.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS,
)

xnnpack_unit_test(
    name = "f32_roundz_eval",
    srcs = [
        "f32-roundz.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS,
)

xnnpack_unit_test(
    name = "f32_tanh_eval",
    srcs = [
        "f32-tanh.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS + [
        ":math_evaluation_tester",
    ],
)

xnnpack_unit_test(
    name = "u32_sqrt_eval",
    srcs = [
        "u32-sqrt.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS,
)

xnnpack_unit_test(
    name = "u64_sqrt_eval",
    srcs = [
        "u64-sqrt.cc",
    ],
    automatic = False,
    deps = ACCURACY_EVAL_DEPS,
)
